<link rel="stylesheet" type="text/css" href="../stylesheet/style.css"/>
<?php
/*
* @author Nguyen Xuan Hong
* @copyright 2011
*/

require_once('../includes/game.tbl.php');
require_once('../includes/gamedetail.tbl.php');
require_once('../includes/gamedetailsub.tbl.php');
require_once('../includes/gamedetailnews.tbl.php');

$id = (int)sget('id');
$game_id = (int)sget('gameid');
$gamedetail_id = (int)sget('gamedetailid');
$gamedetailsub_id = (int)sget('gamedetailsubid');
if($mode == 'listgame' || $mode == 'editgame')
{
	$pClsGame = new clsGameTable();
	
	if($mode == 'listgame')
	{
		if($pClsGame->avaibleSubmit())
		{
			$pClsGame->loadBuffer();
			$pClsGame->insertDB();
			echo acpredirect("Đã thêm Game");
			exit();
		}
		echo $pClsGame->createForm('Thêm Game mới');
	}
	else if($mode == 'editgame')
	{
		if($pClsGame->avaibleSubmit())
		{
			$pClsGame->loadBuffer();
			$pClsGame->updateDB('id',$id);
			echo acpredirect("Đã cập nhật Game", 'page=game&mode=listgame');
			exit();
		}
		$pClsGame->loadDB('id',$id);
		echo $pClsGame->createForm("Sửa thông tin Game '".$pClsGame->value('name')."'");
	}
	
	createHeaderForm('Danh sách Game');
	$list_service_type = $pClsGame->listobj();
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstgame',array("STT","Tên Game","DS chi tiết","Sửa","Xóa"));
	$i = 0;
	if($list_service_type)
	{
		foreach($list_service_type as $game)
		{
			$i++;
			$html_detail = '<a href="?page=game&gameid='.$game->value('id').'&mode=listgamedetail">Chi tiết</a>';	
			$html_edit   = '<a href="?page=game&mode=editgame&id='.$game->value('id').'">Sửa</a>';	
			$html_delete = '<a href="?page=game&mode=deletegame&id='.$game->value('id').'">Xóa</a>';
			$table->addRow($i, array($i, $game->value('name'), $html_detail, $html_edit, $html_delete));
		}
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deletegame')
{		
	// Redefine insert
	$pClsGame = new clsGameTable($id);
	if($_POST['submit'])
	{
		$pClsGame->removeDB('id', $id);
		echo acpredirect('Đã xoá game', 'page=game&mode=listgame');
		exit();
	}
	else if($_POST['nosubmit'])
	{
		echo acpredirect('Trở về', 'page=game&mode=listgame');
		exit();
	}
	
	createDeleteForm("Game '".$pClsGame->value('name')."'");   
	exit();
}
//------------------------------------------------------------------------------------------

if($mode == 'listgamedetail' || $mode == 'editgamedetail')
{
	$pClsGameDetail = new clsGameDetailTable();
	$pClsGame = new clsGameTable($game_id);
	
	if($mode == 'listgamedetail')
	{
		if($pClsGameDetail->avaibleSubmit())
		{
			$pClsGameDetail->loadBuffer();
			$pClsGameDetail->setvalue('game_id', $game_id);
			$pClsGameDetail->insertDB();
			echo acpredirect("Đã thêm Chi tiết game");
			exit();
		}
		echo $pClsGameDetail->createForm('Thêm Chi tiết game '.$pClsGame->value('name'));
	}
	else if($mode == 'editgamedetail')
	{
		if($pClsGameDetail->avaibleSubmit())
		{
			$pClsGameDetail->loadBuffer();
			$pClsGameDetail->updateDB('id',$id);
			echo acpredirect("Đã cập nhật Chi tiết game", 'page=game&gameid='.$game_id.'&mode=listgamedetail');
			exit();
		}
		$pClsGameDetail->loadDB('id',$id);
		echo $pClsGameDetail->createForm("Sửa thông tin Chi tiết game '".$pClsGameDetail->value('name')."'");
	}
	
	createHeaderForm('Danh sách Chi tiết game '.$pClsGame->value('name'));
	$list_gamedetail = $pClsGameDetail->listobj($game_id);
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstgamedetail',array("STT","Chi tiết","DS Chi tiết con","DS Bản tin","Sửa","Xóa"));
	$i = 0;
	if($list_gamedetail)
	{
		foreach($list_gamedetail as $gamedetail)
		{
			$i++;
			$html_detail = '<a href="?page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail->value('id').'&mode=listgamedetailsub">Chi tiết con</a>';
			$html_news =   '<a href="?page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail->value('id').'&mode=listgamedetailnews">Tin tức</a>';
			$html_edit =   '<a href="?page=game&gameid='.$game_id.'&mode=editgamedetail&id='.$gamedetail->value('id').'">Sửa</a>';	
			$html_delete = '<a href="?page=game&gameid='.$game_id.'&mode=deletegamedetail&id='.$gamedetail->value('id').'">Xóa</a>';
			$table->addRow($i, array($i, $gamedetail->value('name'), $html_detail, $html_news, $html_edit, $html_delete));
		}
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deletegamedetail')
{		
	// Redefine insert
	$pClsGameDetail = new clsGameDetailTable($id);
	if($_POST['submit'])
	{
		$pClsGameDetail->removeDB('id', $id);
		echo acpredirect('Đã xoá Chi tiết game', 'page=game&gameid='.$game_id.'&mode=listgamedetail');
		exit();
	}
	else if($_POST['nosubmit'])
	{
		echo acpredirect('Trở về', 'page=game&gameid='.$game_id.'&mode=listgamedetail');
		exit();
	}
	
	createDeleteForm("Chi tiết game '".$pClsGameDetail->value('name')."'");   
	exit();
}

//------------------------------------------------------------------------------------------

if($mode == 'listgamedetailsub' || $mode == 'editgamedetailsub')
{
	$pClsGame = new clsGameTable($game_id);
	$pClsGameDetail = new clsGameDetailTable($detail_id);
		
	$pClsGameDetailSub = new clsGameDetailSubTable();
	if($mode == 'listgamedetailsub')
	{
		if($pClsGameDetailSub->avaibleSubmit())
		{
			$pClsGameDetailSub->loadBuffer();
			$pClsGameDetailSub->setvalue('detail_id', $gamedetail_id);
			$pClsGameDetailSub->insertDB();
			echo acpredirect("Đã thêm Chi tiết game");
			exit();
		}
		echo $pClsGameDetailSub->createForm('Thêm Chi tiết con');
	}
	else if($mode == 'editgamedetailsub')
	{
		if($pClsGameDetailSub->avaibleSubmit())
		{
			$pClsGameDetailSub->loadBuffer();
			$pClsGameDetailSub->updateDB('id',$id);
			echo acpredirect("Đã cập nhật Chi tiết game", 'page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&mode=listgamedetailsub');
			exit();
		}
		$pClsGameDetailSub->loadDB('id',$id);
		echo $pClsGameDetailSub->createForm("Sửa thông tin Chi tiết game '".$pClsGameDetailSub->value('name')."'");
	}
	
	createHeaderForm('Danh sách Chi tiết con');
	$list_gamedetailsub = $pClsGameDetailSub->listobj($gamedetail_id);
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstgamedetailsub',array("STT","Chi tiết game","DS Bản tin","Sửa","Xóa"));
	$i = 0;
	if($list_gamedetailsub)
	{
		foreach($list_gamedetailsub as $gamedetailsub)
		{
			$i++;
			$html_news =   '<a href="?page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&gamedetailsubid='.$gamedetailsub->value('id').'&mode=listgamedetailnews">Tin tức</a>';	
			$html_edit =   '<a href="?page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&mode=editgamedetailsub&id='.$gamedetailsub->value('id').'">Sửa</a>';	
			$html_delete = '<a href="?page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&mode=deletegamedetailsub&id='.$gamedetailsub->value('id').'">Xóa</a>';
			$table->addRow($i, array($i, $gamedetailsub->value('name'), $html_news, $html_edit, $html_delete));
		}
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deletegamedetailsub')
{		
	// Redefine insert
	$pClsGameDetailSub = new clsGameDetailSubTable($id);
	if($_POST['submit'])
	{
		$pClsGameDetailSub->removeDB('id', $id);
		echo acpredirect('Đã xoá Chi tiết game', 'page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&mode=listgamedetailsub');
		exit();
	}
	else if($_POST['nosubmit'])
	{
		echo acpredirect('Trở về', 'page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&mode=listgamedetailsub');
		exit();
	}
	
	createDeleteForm("Chi tiết game '".$pClsGameDetailSub->value('name')."'");   
	exit();
}

//------------------------------------------------------------------------------------------

if($mode == 'listgamedetailnews' || $mode == 'editgamedetailnews')
{
	$pClsGameDetailNews = new clsGameDetailNewsTable();
	if($mode == 'listgamedetailnews')
	{
		if($pClsGameDetailNews->avaibleSubmit())
		{
			$pClsGameDetailNews->loadBuffer();
			$pClsGameDetailNews->setvalue('keyword', convert_url($pClsGameDetailNews->value('title')));
			if($gamedetailsub_id)
			{
				$pClsGameDetailNews->setvalue('detail_id', $gamedetailsub_id);
				$pClsGameDetailNews->setvalue('subdetail', 1);
			}	
			else
			{
				$pClsGameDetailNews->setvalue('detail_id', $gamedetail_id);
				$pClsGameDetailNews->setvalue('subdetail', 0);
			}
				
			$pClsGameDetailNews->insertDB();
			
			$id = $pClsGameDetailNews->value('id');
   		
			$image = upload_file("Upload/", 'imgbrowser_image', $pClsGameDetailNews->value('image'), $id);
			if($image != "")
			{
				$pClsGameDetailNews->setvalue('image', $image);
			} 
			
			$pClsGameDetailNews->updateDB('id',$id);
			
			echo acpredirect("Đã thêm Bản tin Game");
			exit();
		}
		echo $pClsGameDetailNews->createForm('Thêm Bản tin Game');
	}
	else if($mode == 'editgamedetailnews')
	{
		if($pClsGameDetailNews->avaibleSubmit())
		{
			$pClsGameDetailNews->loadBuffer();
			$pClsGameDetailNews->setvalue('keyword', convert_url($pClsGameDetailNews->value('title')));
			
			$image = upload_file("Upload/", 'imgbrowser_image', $pClsGameDetailNews->value('image'), $id);
			if($image != "")
			{
        		$pClsGameDetailNews->setvalue('image', $image);
			} 
			
			$pClsGameDetailNews->updateDB('id',$id);
			if($gamedetailsub_id)
			{
				echo acpredirect("Đã cập nhật Bản tin Game", 'page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&gamedetailsubid='.$gamedetailsub_id.'&mode=listgamedetailnews');
			}
			else
			{
				echo acpredirect("Đã cập nhật Bản tin Game", 'page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&mode=listgamedetailnews');
			}
			exit();
		}
		$pClsGameDetailNews->loadDB('id',$id);
		echo $pClsGameDetailNews->createForm("Sửa bản tin Dịch vụ '".$pClsGameDetailNews->value('name')."'");
	}
	
	$pClsGame = new clsGameTable($game_id);
	createHeaderForm('Danh sách Bản tin Chi tiết '.$pClsGame->value('name'));
	
	if($gamedetailsub_id)
		$list_gamedetailnews = $pClsGameDetailNews->listobj($gamedetailsub_id, 1, '', 100);
	else
		$list_gamedetailnews = $pClsGameDetailNews->listobj($gamedetail_id, 0, '', 100);
	
	require_once('../includes/templates.php');
	$table = new clsTable('lstgamedetailnews',array("STT","Bản tin","Sửa","Xóa"));
	$i = 0;
	if($list_gamedetailnews)
	{
		foreach($list_gamedetailnews as $gamedetailnews)
		{
			$i++;
			if($gamedetailsub_id)
			{
				$html_edit =   '<a href="?page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&gamedetailsubid='.$gamedetailsub_id.'&mode=editgamedetailnews&id='.$gamedetailnews->value('id').'">Sửa</a>';	
				$html_delete = '<a href="?page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&gamedetailsubid='.$gamedetailsub_id.'&mode=deletegamedetailnews&id='.$gamedetailnews->value('id').'">Xóa</a>';
			}
			else
			{
				$html_edit =   '<a href="?page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&mode=editgamedetailnews&id='.$gamedetailnews->value('id').'">Sửa</a>';	
				$html_delete = '<a href="?page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&mode=deletegamedetailnews&id='.$gamedetailnews->value('id').'">Xóa</a>';
				
			}
			
			$table->addRow($i, array($i, $gamedetailnews->value('title'), $html_edit, $html_delete));
		}
	}
	echo $table->putHTML(true);
	exit();
}

if($_GET['mode'] == 'deletegamedetailnews')
{		
	// Redefine insert
	$pClsGameDetailNews = new clsGameDetailNewsTable($id);
	if($_POST['submit'])
	{
		$pClsGameDetailNews->removeDB('id', $id);
		if($gamedetailsub_id)
		{
			echo acpredirect("Đã xoá Bản tin Game", 'page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&gamedetailsubid='.$gamedetailsub_id.'&mode=listgamedetailnews');
		}
		else
		{
			echo acpredirect("Đã xoá Bản tin Game", 'page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&mode=listgamedetailnews');
		}
		exit();
	}
	else if($_POST['nosubmit'])
	{
		if($gamedetailsub_id)
		{
			echo acpredirect("Trở về", 'page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&gamedetailsubid='.$gamedetailsub_id.'&mode=listgamedetailnews');
		}
		else
		{
			echo acpredirect("Trở về", 'page=game&gameid='.$game_id.'&gamedetailid='.$gamedetail_id.'&mode=listgamedetailnews');
		}
		exit();
	}
	
	createDeleteForm("Bản tin Game '".$pClsGameDetailNews->value('name'));   
	exit();
}

